#include "widget.h"
#include "ui_widget.h"
#include<QMessageBox>
#include"handler.h"
#include"myscene.h"
#include"rules.h"
Widget::Widget(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //new出一个场景对象
    this->sc=new MyScene;
    //把场景放入ui上的graphicsView容器中
    this->ui->graphicsView->setScene(sc);
    connect(this->ui->button_start,SIGNAL(clicked(bool)),this->sc,SLOT(startGame()));

    connect(this->ui->button_end,SIGNAL(clicked(bool)),this->sc,SLOT(endGame()));

    Handler *hand=Handler::getInstance();
    connect(hand,SIGNAL(beatMouse()),this,SLOT(UpdateScore1()));
    connect(hand,SIGNAL(beatnoneMouse()),this,SLOT(UpdateScore2()));
    connect(hand,SIGNAL(returntozero()),this,SLOT(setscore()));
    connect(hand,SIGNAL(resettime()),this,SLOT(settime()));
    connect(hand,SIGNAL(stoptime()),this,SLOT(stoptime1()));

    this->r=new Rules;
    connect(this->ui->Rules,SIGNAL(clicked(bool)),this->r,SLOT(show()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::closeEvent(QCloseEvent *event)
{
    bool ret=QMessageBox::question(this,"确定","你真的要关闭吗？","是","否");
    if(ret)
    {
        event->ignore();
    }
}//关闭程序时弹出的窗口

void Widget::UpdateScore1()
{
    this->score1+=1;
    this->ui->score_get->display(this->score1);
}

void Widget::UpdateScore2()
{
    this->score2-=1;
    this->ui->score_lose->display(this->score2);
}

void Widget::setscore()
{
    score1=0;
    score2=0;
    this->ui->score_get->display(this->score1);
    this->ui->score_lose->display(this->score2);
}

void Widget::settime()
{
    time=60;
    this->ui->clock->display(this->time);
    this->timer1=new QTimer;
    this->timer1->start(1000);
    connect(this->timer1,SIGNAL(timeout()),this,SLOT(UpdateTime()));
}

void Widget::UpdateTime()
{
    this->time-=1;
    if(time==0)
    {
        this->timer1->stop();
        delete timer1;
        sc->endGame();
    }
    this->ui->clock->display(this->time);
}

void Widget::stoptime1()
{
    this->timer1->stop();
    delete timer1;
}
//over!!!!！